<template>
	<view class="editInfo" v-if="msg.name">
		<user-certs v-if="msg.name == 'userCerts'" :list="msg.value"></user-certs>
		<phone :phone-num="msg.value" v-else-if="msg.name == 'phone'"></phone>
		<edit-info-item :name="msg" :key-name="msg.name" @update="handleUpdate" v-else></edit-info-item>
		<previewMedia ref="previewMedia"></previewMedia>
	</view>
</template>

<script>
import EditInfoItem from './components/edit_info_item.vue';
import UserCerts from './components/userCerts/index.vue'; // 人员证件
import phone from './components/phone/index.vue'; // 修改电话号码
import { hanleToken } from '@/utils/utils.js';
import { update,updateAvatar } from '@/apis/personalCenter.js';
import { mapState, mapActions } from 'vuex';
export default {
	components: {
		EditInfoItem,
		UserCerts,
		phone
	},
	data() {
		return {
			msg: {},
			titleMap: {
				avatar: '个人信息编辑',
				showInfo: '隐私设置',
				phone: '修改手机',
				vehicleName: '选择交通工具',
				userCerts: '人员证件'
			}
		};
	},
	onLoad(res) {
		// this.msg = JSON.parse(res.msg); 
		const msg = uni.getStorageSync('personalInfo_edit_msg');
		this.msg = msg ? JSON.parse(msg) : {};
		uni.setNavigationBarTitle({
			title: this.titleMap[this.msg.name]
		});
		// #ifdef H5
		document.title = this.titleMap[this.msg.name];
		// #endif
	},
	computed:{
		...mapState('user', ['userinfo']),
	},
	methods: {
		handleUpdate(obj) {
			uni.showLoading({
				title: '请求中'
			});
			// console.log(obj,"obj的值",this.msg);
			// return
			let { address, id, longitude, latitude, vehicle } = this.msg;
			const baseParam = { address, id, longitude, latitude, vehicle, ...obj };
			update(baseParam).then(res => {
				// console.log(res,'修改成功')
				if (res) {
					uni.hideLoading();
					uni.showToast({
						title: '修改成功',
						icon: 'none'
					});
					// 如果是姓名和电话号码需要修改缓存
					if (this.msg.name == 'realName' || this.msg.name == 'phone') {
						// let userData = hanleToken('get', 'userData');
						// if (this.msg.name == 'realName') {
						// 	userData.nick_name = obj.realName;
						// } else {
						// 	userData.phone = obj.phone;
						// }
						// hanleToken('set', 'userData', userData);
						let userData = this.userinfo;
						if (this.msg.name == 'realName') {
							userData.nick_name = obj.realName;
						} else {
							userData.phone = obj.phone;
						}
						this.$store.commit('user/setUserinfo',userData);
					}
				   uni.navigateBack({});
				}
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.editInfo {
	min-height: 100vh;
	background-color: #f3f6f8;
}
</style>
